<?php
namespace app\controllers;
use app\models\ApplyModel;
use app\models\CourseModel;
use wlcat\http\controllers\Controller;
class CountController extends Controller{
    public function index()
    {
        $this->assignFormToken();
        $this->display();
    }

    public function count()
    {
        $this->checkFormToken();

        $data  = $this->checkFormData();
        $height = (int)$data['height'];
        $weight = round($data['weight'], 2);
        $age    = (int)$data['age'];
        $add    = $addPre = (float)$data['add'] <=0 ? 0 : round($data['add'], 2);
        $cycle  = $data['cycle'];
        $countType = (int)$data['countType'];
        $sex    = (int)$data['sex'];

        $add    = round(7700*$add/30, 2);  //一公斤7700热量，除以30天
        if ($countType === 2)
        {
            $add = (- $add);
        }

        //根据性别获取卡路里
        $b = 0;
        switch ($sex)
        {
            case '1':
                $b = $this->getManCalory($weight, $height, $age);
                break;
            case '2' :
                $b = $this->getWoManCalory($weight, $height, $age);
                break;
            default:
                return $this->error('请选择年龄');
        }


        $b = 66 + 13.7*$weight + 5*$height - 6.8*$age ;

        $a = $b * $cycle + $add;

        $tanshui = $a * 0.6 / 4;
        $danbai = $a * 0.2 / 4;
        $zhifang = $a * 0.2 / 9;
        $this->assign('b', $b);
        $this->assign('a', round($b * $cycle , 2));
        $this->assign('add', abs($add));
        $this->assign('total', $a);
        $this->assign('danbai', round($danbai,2));
        $this->assign('zhifang', round($zhifang,2));
        $this->assign('tanshui', round($tanshui,2));
        $this->assign('countType', $countType == 1?"增肌" : "减肥");
        $this->assign('ct', $countType == 1?"大于" : "小于");
        $this->assign('aim', $addPre);

        $this->display();
    }

    /**获取男人卡路里
     * @param $weight
     * @param $height
     * @param $age
     * @return float|int
     */
    protected function getManCalory($weight, $height, $age)
    {
        return 66 + 13.7*$weight + 5*$height - 6.8*$age ;
    }

    /**获取女人卡路里
     * @param $weight
     * @param $height
     * @param $age
     * @return float|int
     */
    protected function getWoManCalory($weight, $height, $age)
    {
        return 655 +  (9.6 *$weight) + (1.7*$height) - (4.7*$age);
    }

    public function checkFormData()
    {
        $data = $this->request->input('post.');
        $fields = [
            'add' => [
                'method' => 'float',
                'tip'    => '消耗热量',
                'unable' => true
            ],
            'height' => [
                'method' => 'float',
                'tip'    => '身高',

            ],
            'weight' => [
                'method' => 'float',
                'tip'    => '重量',

            ],
            'age' => [
                'method' => 'int',
                'tip'    => '年龄',

            ],
            'cycle' => [
                'method' => 'in',
                'tip'    => '周期',
                'limit'    => ['1.15', '1.375', '1.55', '1.725']
            ],

            'sex' => [
                'method' => 'in',
                'tip'    => '性别',
                'limit'    => ['1', '2']
            ],
            'countType' => [
                'method' => 'in',
                'tip'    => '增肌/减肥类型',
                'limit'    => ['1', '2']
            ],
        ];

        foreach ($fields as $key => $limit)
        {
            if (!isset($data[$key]))
            {
                return $this->error('不存在: '.$limit['tip'], 3);

            }
            $tips = $limit['tip'].'请重新写';

            switch ($limit['method'])
            {
                case 'float':
                    $data[$key] = (float)$data[$key];
                    if (empty($data[$key]) && (!isset($limit['unable']) || $limit['unable'] === false))
                    {
                        return $this->error($tips, 3);
                    }
                    break;
                case 'int':
                    $data[$key] = (int)$data[$key];
                    if (empty($data[$key]))
                    {
                        return $this->error($tips, 3);

                    }
                    break;
                case 'in':
                    if (!in_array($data[$key], $limit['limit']))
                    {
                        return $this->error($tips, 3);
                        die;
                    }
                    break;
            }
        }
        return $data;
    }
}